package cn.woblog.android.http;

import android.app.Activity;

import com.alibaba.fastjson.JSON;

import org.apache.http.Header;

import java.lang.reflect.ParameterizedType;

import cn.woblog.android.common.utils.ToastAlone;

/**
 * Created by ren on 2015/6/3 0003.
 */
public abstract class JsonHandler<T> extends TextHttpResponseHandler{
    private final Class<T> entityClass;
    private boolean isArray;
    private CacheConfig cacheConfig = new CacheConfig();
    private String cacheKey;

    public JsonHandler(boolean isArray){
        this();
        this.isArray = isArray;
    }
    public JsonHandler(){
        java.lang.reflect.Type genType = getClass().getGenericSuperclass();
        ParameterizedType type = (ParameterizedType) genType;
        Class<T> clazz = (Class<T>) type.getActualTypeArguments()[0].getClass();
        if (ParameterizedType.class.isAssignableFrom(clazz)) {
            ParameterizedType type2 = (ParameterizedType) type.getActualTypeArguments()[0];
            entityClass = (Class<T>) type2.getActualTypeArguments()[0];
        } else {
            entityClass = (Class<T>) type.getActualTypeArguments()[0];
        }
    }
    @Override
    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
        if (cacheKey!=null){
            String cacheString = cacheConfig.getCacheString(getRequestURI().toString(), cacheKey);
            if (cacheString!=null){
                onReadCacheData();
                success(-100, headers, parse(cacheString));
            }else {
                fail(-101,headers,responseString,throwable);
            }
        }else{
            fail(statusCode,headers,responseString,throwable);
        }
    }

    protected void onReadCacheData() {
        ToastAlone.showSortToast((Activity) Http.getContext(), "当前无网络只能使用离线内容");
    }

    /**
     *
     * @param statusCode -101 读取缓存为空
     * @param headers
     * @param responseString
     * @param throwable
     */
    protected abstract void fail(int statusCode, Header[] headers, String responseString, Throwable throwable);
    /**
     *
     * @param statusCode -100 读取缓存也为空
     * @param headers
     * @param data
     */
    protected abstract void success(int statusCode, Header[] headers, T data);
    @Override
    public void onSuccess(int statusCode, Header[] headers, String responseString) {
        if (responseString!=null){
            if (cacheKey!=null){
                cacheConfig.setCacheString(getRequestURI().toString(),cacheKey,responseString);
            }
            success(statusCode, headers, parse(responseString));
        }
    }

    private T parse(String responseString) {
        T t= null;
        if (isArray) {
            t = (T) JSON.parseArray(responseString, entityClass);
        } else {
            t = JSON.parseObject(responseString, entityClass);
        }
        return t;
    }

    public ResponseHandlerInterface cache(){
        this.cacheKey = "default";
        return  this;
    }public ResponseHandlerInterface cache(String cacheKey){
        this.cacheKey = cacheKey;
        return  this;
    }

    @Override
    public void onFinish() {
        super.onFinish();
        finish();
    }

    protected void finish() {

    }
}
